<?php

namespace App\Services;
use APP\Libraries\Tool;
use Illuminate\Support\Facades\DB;
use App\User;
class TestService
{

    public function login($username,$type)
    {
        $Tool = new Tool();
        if($type=='admin'){
            $user = User::where('account',$username)->where('u_g_type','admin')->where('islocked',2)->first();
        }
        if($type=='saleB'){
            $user = User::where('account',$username)->where('u_g_type','sales')->where('islocked',2)->first();
        }
        if($type=='supplyB'){
            $user = User::where('account',$username)->where('u_g_type','supplier')->where('islocked',2)->first();
        }

        if($user){
            return $user;
        }else{
            return 'no';
        }
    }
    public function role_index($arr_page)
    {
        $Tool = new Tool();
        $array=array();
        if($arr_page!=[]){
            for($a=0;$a<count($arr_page);$a++){
                $array[$a]['ic']=$arr_page[$a]->ic;
                $array[$a]['name']=$arr_page[$a]->title;
                $array[$a]['key']=$arr_page[$a]->mytype;
                $array[$a]['remarks']=$arr_page[$a]->explain;
                $array[$a]['createat']=$Tool->datas(2,$arr_page[$a]->created_at);
            }
        }
        return $array;
    }

    public function account_index($arr_page)
    {
        $Tool = new Tool();
        $array=array();
        if($arr_page!=[]){
            for($a=0;$a<count($arr_page);$a++){
                $array[$a]['ic']=$arr_page[$a]->ic;
                $array[$a]['nick']=$arr_page[$a]->nick;
                $array[$a]['account']=$arr_page[$a]->account;
                $array[$a]['bus_ic']=$arr_page[$a]->s_ic;
                if($arr_page[$a]->s_ic){
                    $array[$a]['bus']=$Tool->index_field('shopers','ic',$arr_page[$a]->s_ic,'title');
                }
                $array[$a]['authority']=explode('/',$arr_page[$a]->power);
                $array[$a]['remarks']=$arr_page[$a]->explain;
                $array[$a]['state']=$arr_page[$a]->islocked;
                $array[$a]['createat']=$Tool->datas(2,$arr_page[$a]->created_at);
            }
        }
        return $array;
    }

    public function account_operation_sub($ic,$input)
    {
        $Tool = new Tool();
        $date =$Tool->datas(1,'');
        $array=array();
        $array['u_g_type'] = $input['role_key'];
        $array['islocked'] = $input['state'];
        $array['explain'] = $input['remarks'];
        $array['power'] = implode('/',$input['power']);
        $array['nick'] = $input['nick'];
        $array['account'] = $input['account'];
        if($input['role_key']!='admin'){
            $array['s_ic'] = $input['business_ic'];
        }
        if ($ic['ic']!=''){//编辑
            $input['updated_at'] = $date;
            $Tool->mysql_edit('users',$ic['ic'],$array);
            return 'new';
        }else{//新建
            $array['ic']=getfirstic();
            $array['password'] = bcrypt('666666');
            $array['created_at'] = $date;
            $Tool->mysql_insert('users',$array);
            return 'edit';
        }
    }

    public function get_business($arr_page)
    {
        $Tool = new Tool();
        $array=array();
        if($arr_page!=[]){
            for($a=0;$a<count($arr_page);$a++){
                $array[$a]['ic']=$arr_page[$a]->ic;
                $array[$a]['label']=$arr_page[$a]->title;
            }
        }
        return $array;
    }

    public function parameter_setting($input)
    {
        $Tool = new Tool();
        $date =$Tool->datas(1,'');
        $input['ic']=getfirstic();
        $input['after_sale'] = implode('/',$input['after_sale']);
        $input['recharge'] = implode('/',$input['recharge']);
        $input['notice'] = implode('/',$input['notice']);
        $input['hot_word'] = implode('/',$input['hot_word']);
        $input['created_at'] = $date;
        $system_setup=$Tool->first('system_setup');
        if($system_setup){
            return $Tool->mysql_edit('system_setup',$system_setup->ic,$input);
        }else{
            return $Tool->mysql_insert('system_setup',$input);
        }

    }

    public function platform_message_new($input)
    {
        $Tool = new Tool();
        $date =$Tool->datas(1,'');
        $input['ic']=getfirstic();
        $input['state'] = 2;
        $input['created_at'] = $date;
        $input['group'] = implode('/',$input['group']);;
        $input['time'] = time()*1000;
        return $Tool->mysql_insert('notices',$input);
    }

    public function platform_message_index($arr_page)
    {
        $array=array();
        if($arr_page!=[]){
            for($a=0;$a<count($arr_page);$a++){
                $array[$a]['ic']=$arr_page[$a]->ic;
                $array[$a]['type']=$arr_page[$a]->type;
                $array[$a]['content']=$arr_page[$a]->content;
                $array[$a]['name']=$arr_page[$a]->name;
                $array[$a]['send_group']=explode('/',$arr_page[$a]->group);
                $array[$a]['createat']=$arr_page[$a]->time;
            }
        }
        return $array;
    }
}